package com.whq.websocket;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;

public class WebSocketHandshakeInterceptor implements HandshakeInterceptor{
	private static final Logger logger = LoggerFactory.getLogger(WebSocketHandshakeInterceptor.class);
	@Override
	public void afterHandshake(ServerHttpRequest arg0, ServerHttpResponse arg1, WebSocketHandler arg2, Exception arg3) {
	  		
	}

	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
		logger.info(request.getURI().getQuery());
		String userName = ((ServletServerHttpRequest)request).getServletRequest().getParameter("userName");
		logger.info("==========websocket====token======================="+userName);
		logger.info("websocket >>>> param>>>>>>>",userName);
		if(StringUtils.isNotBlank(userName)){
			attributes.put(userName,userName);
			return true;
		}
       return false;
	}

}
